﻿//
// ProgrammerViewModel.cs
//
// Authors:
//    Anders Gade Nørskov <09549@iha.dk
//    Lars Rønde Hansen <09548@iha.dk>
//    Claus Jørgensen <10229@iha.dk>
//
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows.Input;
using System.Collections.ObjectModel;

using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;

namespace RSD.ViewModel
{
    using Data;
    using Scripting;

    public class ProgrammerViewModel : ViewModelBase
    {
        private readonly IScriptRepository scriptRepository;
        private readonly IScorBotScript scorbotScript;

        private readonly RelayCommand saveCommand;
        private readonly RelayCommand newCommand;
        private readonly RelayCommand checkSyntaxCommand;

        private string errorMessage;
        private Script selectedScript = new Script();

        public ProgrammerViewModel(IScriptRepository scriptRepository, IScorBotScript scorbotScript)
        {
            this.scriptRepository = scriptRepository;
            this.scorbotScript = scorbotScript;

            this.saveCommand = new RelayCommand(SaveExecute);
            this.newCommand = new RelayCommand(NewExecute);
            this.checkSyntaxCommand = new RelayCommand(CheckSyntaxExecute);

            this.Scripts = new ObservableCollection<Script>(scriptRepository.FindAll());
        }

        #region Properties

        public ObservableCollection<Script> Scripts
        {
            get;
            private set;
        }

        public Script SelectedScript
        {
            get
            {
                return selectedScript;
            }
            set
            {
                selectedScript = value;
                RaisePropertyChanged("SelectedScript");
            }
        }

        public string ErrorMessage
        {
            get
            {
                return errorMessage;
            }
            set
            {
                errorMessage = value;
                RaisePropertyChanged("ErrorMessage");
            }
        }

        public ICommand SaveCommand
        {
            get
            {
                return saveCommand;
            }
        }

        public ICommand NewCommand
        {
            get
            {
                return newCommand;
            }
        }

        public ICommand CheckSyntaxCommand
        {
            get
            {
                return checkSyntaxCommand;
            }
        }

        #endregion

        private void SaveExecute()
        {
            if (SelectedScript.ScriptId != 0)
            {
                scriptRepository.SaveChanges();
            }
            else
            {
                scriptRepository.Add(SelectedScript).SaveChanges();
                Scripts.Add(SelectedScript);
            }            
        }

        private void NewExecute()
        {
            SelectedScript = new Script();
        }

        private void CheckSyntaxExecute()
        {
            if (SelectedScript != null)
            {
                ErrorMessage = "";
                
                scorbotScript.CheckScriptAsync(SelectedScript.Code, e =>
                {
                    if (e != null)
                    {
                        ErrorMessage = string.Format("Validation: Error: [{0}]", e.Message);
                    }
                    else
                    {
                        ErrorMessage = "Validation: Valid.";
                    }
                });
            }
        }
    }
}
